/***     
 * 找对子工具类
 */

var _ = require('./../../FrameWork/we_underscore');
var FindSameCardValueTool = require('./FindSameCardValueTool');
var FindShunZiTool = require('./FindShunZiTool');
var EumPkGroup = require('./../Enum/EumPkGroup');
var FindPairsTool = module.exports = {};

/***是否为一对 */
FindPairsTool.isOnePairs = function(_cardInfoList) {
    var size = _.size(_cardInfoList);
    if (2 != size) {
        return false;
    }
    var isFind = _cardInfoList[0].value == _cardInfoList[1].value;
    return isFind;
};

/***
 */
FindPairsTool.getResult = function(_cardInfoList) {
    var result = this.check(_cardInfoList);
    return result;
};

FindPairsTool.check = function(_cardInfoList) {
    var cardGroupList = FindSameCardValueTool.check(_cardInfoList);
    var result = {
        isFind: false,
        otherGroup: [],
    }

    var otherGroup = []; //无法组成对的牌
    var canPaitGroup = []; //可以组成对的牌
    _.each(cardGroupList, function(_sameCardGroup) {
        var size = _.size(_sameCardGroup);
        if (size == 1) {
            otherGroup.push(_sameCardGroup);
        } else {
            result.isFind = true;
            canPaitGroup.push(_sameCardGroup);
        }
    });

    //存储一对一对
    var singlePairsList = [];

    _.each(canPaitGroup, function(_sameCardGroup) {
        var size = _.size(_sameCardGroup);
        if (2 == size) {
            singlePairsList.push(_sameCardGroup);
        } else if (3 == size) {
            var tmpGroupList = _.partition(_sameCardGroup, function(info, idx) {
                return idx >= 1;
            })
            singlePairsList.push(tmpGroupList[0]);
            otherGroup.push(tmpGroupList[1]);
        } else if (4 == size) {
            var tmpGroupList = _.partition(_sameCardGroup, function(info, idx) {
                return idx >= 1;
            })
            singlePairsList.push(tmpGroupList[0]);
            singlePairsList.push(tmpGroupList[1]);
        } else if (5 == size) {
            var tmpGroupList = _.partition(_sameCardGroup, function(info, idx) {
                return idx >= 1;
            })
            singlePairsList.push(tmpGroupList[0]);

            tmpGroupList = _.partition(tmpGroupList[1], function(info, idx) {
                return idx >= 1;
            })
            singlePairsList.push(tmpGroupList[0]);
            otherGroup.push(tmpGroupList[1]);
        }
    })

    otherGroup = _.flatten(otherGroup);
    result.otherGroup = otherGroup;
    result.findGroup = singlePairsList;
    //分离能成对的牌
    return result;
};
/**连对
 * _cardInfoList:[],
 * _pairsCnt:xx//对子数量 默认要求至少3对
 */
FindPairsTool.isEvenPair = function(_cardInfoList, _pairsCnt) {
    var size = _.size(_cardInfoList);
    _pairsCnt = null == _pairsCnt ? 3 : _pairsCnt;
    if (size < _pairsCnt * 2) {
        return false;
    }

    /***是否全部成对 */
    var tmpCards = FindSameCardValueTool.check(_cardInfoList);
    if (size != _.size(tmpCards) * 2) {
        return false;
    }

    var shunInfo = _.map(tmpCards, function(_pairsInfo) {
        return _pairsInfo[0]
    }.bind(this));
    shunInfo = _.compact(shunInfo);

    if (true == FindShunZiTool.isShunZi(shunInfo, _.size(shunInfo))) {
        return true;
    }
    return false;
};

/**连对
 * _cardInfoList:[],
 * _pairsCntMin:xx//连对的最低对子数 
 */
FindPairsTool.getEvenPairMax = function(_cardInfoList, _pairsCntMin) {
    _pairsCntMin = null == _pairsCntMin ? 3 : _pairsCntMin;
    var result = this.getResult(_cardInfoList);
    if (result.isFind) {
        var findGroup = result.findGroup;
        var findGroupSize = _.size(findGroup);

        /*到这还只是可能组合成连对 */
        if (findGroupSize >= _pairsCntMin) {

            var singleCards = _.map(findGroup, function(_list) {
                return _list[0]
            });
            var tmpResult = FindShunZiTool.getResultSection(singleCards, _pairsCntMin, findGroupSize);
            if (tmpResult.isFind) {
                /**确定找到了顺子 */
                var lastFindGroup = [];
                _.each(tmpResult.findGroupMore, function(_singleGroupResult) {
                    _.each(_singleGroupResult.findGroup, function(_tmpCard) {

                        _.each(findGroup, function(list) {
                            if (list[0].value == _tmpCard.value) {
                                lastFindGroup.push(list);
                            }
                        })

                    })
                })

                lastFindGroup = _.flatten(lastFindGroup);
                return {
                    isFind: true,
                    findGroup: lastFindGroup,
                    type: EumPkGroup.EVENPAIRS,
                    cardIds: _.map(lastFindGroup, function(cardInfo) {
                        return cardInfo.id;
                    }),
                    otherGroup: _.difference(_cardInfoList, lastFindGroup)
                }
            }
        }
    }
    return {
        isFind: false,
        findGroup: [],
        otherGroup: _cardInfoList
    }
};